<template>
	<div class="system-dic-dialog-container">
		<el-dialog :title="state.dialog.title" @close="closeDialog" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="menuDialogFormRef" :model="ruleForm" size="default" label-width="120px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="监控杆编号：" prop="number" :rules="rules.number">
							<el-input :disabled="true" size="default"  placeholder="监控杆编号" style="max-width: 400px" v-model="ruleForm.number"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="监控杆名称：" prop="name" :rules="rules.name">
							<el-input size="default" placeholder="监控杆名称" style="max-width: 400px" v-model="ruleForm.name"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="监控杆类型：" prop="type" :rules="rules.type">
							<el-input size="default"  placeholder="监控杆类型" style="max-width: 400px" v-model="ruleForm.type"> </el-input>
						</el-form-item>
						<!-- <el-form-item label="监控杆类型：" prop="type" :rules="rules.type">
							<el-select  placeholder="监控杆类型" style="width: 400px;" v-model="ruleForm.type">
								<el-option label="快速路" :value="0"></el-option>
								<el-option label="主干道" :value="1"></el-option>
								<el-option label="次干道" :value="2"></el-option>
								<el-option label="支路" :value="3"></el-option>
							</el-select>
						</el-form-item> -->
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="安装区域：" prop="region_id" :rules="rules.region_id">
							<el-select clearable filterable  placeholder="安装区域" style="width: 400px;" v-model="ruleForm.region_id">
								<el-option v-for="item in selectData.regionData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="安装道路：" prop="road_id" :rules="rules.road_id">
							<el-select clearable filterable  placeholder="安装道路" style="width: 400px;" v-model="ruleForm.road_id">
								<el-option v-for="item in selectData.roadData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="安装路口：" prop="intersection_id" >
							<el-select clearable filterable  placeholder="安装路口" style="width: 400px;" v-model="ruleForm.intersection_id">
								<el-option   v-for="item in selectData.intersectionData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="安装路段：" prop="location_id" >
							<el-select clearable filterable  placeholder="安装路段" style="width: 400px;" v-model="ruleForm.location_id">
								<el-option v-for="item in selectData.locationData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="安装方向：" prop="fix_direction" >
							<el-select clearable   placeholder="安装方向" style="width: 400px;" v-model="ruleForm.fix_direction">
								<el-option label="-" :value="0"></el-option>
								<el-option label="东" :value="1"></el-option>
								<el-option label="南" :value="2"></el-option>
								<el-option label="西" :value="3"></el-option>
								<el-option label="北" :value="4"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="经度：" prop="lng" >
							<el-input size="default"  placeholder="经度" style="max-width: 400px" v-model="ruleForm.lng"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="维度：" prop="lat" >
							<el-input size="default"  placeholder="维度" style="max-width: 400px" v-model="ruleForm.lat"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="杆位类型：" prop="pole_type" :rules="rules.pole_type">
							<el-select clearable  placeholder="杆位类型" style="width: 400px;" v-model="ruleForm.pole_type">
								<el-option label="新建" :value="1"></el-option>
								<el-option label="利旧" :value="2"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemDicDialog">
import { onMounted, reactive, ref ,watch,defineProps} from 'vue';
import {  ElMessage, type FormRules } from 'element-plus'
import {useBasicApi} from "/@/api/system/menu/index"
import {userApi} from "/@/api/system/user/index"
import {useRoleApi} from "/@/api/system/role/index"
import {deptApi} from "/@/api/system/dept/index"
import {dicApi} from "/@/api/system/dic/index"
const BasicApi = useBasicApi()
const useApi = userApi()
const roleApi = useRoleApi()
const depApi = deptApi()
const diApi = dicApi()
const props = defineProps(['tableData','disabled'])
const disabledFlag = ref<Boolean>(false)
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const searchForm = ref({})
const selectData = reactive({
	regionData:[],
	roadData:[],
	locationData:[],
	intersectionData:[]
})
const getSelectData = (async ()=>{
	searchForm.value.search = `status:1`
	let res = await BasicApi.BasicTableApi(searchForm.value);
	if(res.isSuccess){
		selectData.regionData = res.returnData
	}
	let intersectionRes = await useApi.userTableApi(searchForm.value);
	if(intersectionRes.isSuccess){
		selectData.intersectionData = intersectionRes.returnData
	}
	let roadRes = await roleApi.RoleTableApi(searchForm.value);
	if(roadRes.isSuccess){
		selectData.roadData = roadRes.returnData
	}
	let locationRes = await depApi.getDepList(searchForm.value);
	if(locationRes.isSuccess){
		selectData.locationData = locationRes.returnData
	}
})

watch(props,(newVal,oldVal)=>{
	ruleForm.value = newVal.tableData
	disabledFlag.value = newVal.disabled
})
const menuDialogFormRef = ref(null)
// dx定义start
	interface RuleForm {
		number:string,
		name:string,
		type:string,
		region_id:any,
		road_id:any,
		intersection_id:any,
		location_id:any,
		fix_direction:string,
		lng:string,
		lat:string,
		pole_type:any
	}
	const ruleForm = ref<RuleForm>({
		number:"",
		name:"",
		type:"",
		region_id:null,
		road_id:null,
		intersection_id:null,
		location_id:null,
		fix_direction:"",
		lng:"",
		lat:"",
		pole_type:"",
	})


	const rules = reactive<FormRules<RuleForm>>({
		number:[
			{ required: true, message: '监控杆编号必填', trigger: 'blur' },
		],
		name:[
			{ required: true, message: '监控杆名称必填', trigger: 'blur' },
		],
		type:[
			{ required: true, message: '监控杆类型必填', trigger: 'blur' },
		],
		region_id:[
			{ required: true, message: '安装区域必填', trigger: 'blur' },
		],
		road_id:[
			{ required: true, message: '安装道路必填', trigger: 'blur' },
		],
		pole_type:[
			{ required: true, message: '杆位类型必填', trigger: 'blur' },
		],
	})

onMounted(()=>{
	getSelectData()
})	

// dx定义end
const state = reactive({
	ruleForm: {
		dicName: '', // 字典名称
		fieldName: '', // 字段名
		status: true, // 字典状态
		list: [] as ListType[], // 子集字段 + 属性值
		describe: '', // 字典描述
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = () => {
	
		state.dialog.title = '修改监控杆';
		state.dialog.submitTxt = '修 改'
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	// menuDialogFormRef.value.resetFields()
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = (async() => {
	
		let res = await diApi.editDic(ruleForm.value);
		if(res.isSuccess){
			ElMessage.success('修改成功')
			closeDialog();
			emit('refresh');
		}else{
			ElMessage.error(res.returnMsg)
		}
});


// 暴露变量
defineExpose({
	openDialog,
});
</script>
